package com.deyuanyun.pic.pipe.support.validatefun;

import com.caixu.beanvalidateutil.vinterface.ValidateInterface;
import com.deyuanyun.pic.common.util.ObjectUtil;
import com.deyuanyun.pic.domain.pipe.PipeSegment;
import com.deyuanyun.pic.pipe.support.utils.PipeDictUtils;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;

import java.lang.reflect.Field;

public class PipeSegmentValidata implements ValidateInterface {

	@Override
	public <T, E,C> boolean apply(T t, E e,C c) {
		
		if(ObjectUtil.isNotNullEmpty(t)){
			Field f=(Field) e;//管段
			try {
				String lineName = BeanUtils.getProperty(c,"pipeLineId");
				String segmentName = (String)t;
				String segmentId = PipeDictUtils.getSegmentByName(lineName.trim(),segmentName.trim());
				PipeSegment segment = PipeDictUtils.getSegment(segmentId);

				if (StringUtils.isNotBlank(segmentId)){
					if ("1".equals(segment.getState())){
						return false;
					}
					String lindId=PipeDictUtils.getLineIdByName(lineName);
					BeanUtils.setProperty(c,"pipeLineId",lindId);
					BeanUtils.setProperty(c,f.getName(),segmentId);
				}else {
					return false;
				}


			} catch (Exception e1) {
				e1.printStackTrace();
				return false;
			}

		}
		return true;
	}

	@Override
	public String getName() {
		
		return null;
	}

}
